//
//  EatClassPageViewController.swift
//  MyFood
//
//  Created by 盈灿－刘鑫奇 on 16/2/17.
//  Copyright © 2016年 XRG. All rights reserved.
//

import UIKit
import SwiftyJSON


class EatClassPageViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {

      var eatClassTableView :UITableView!
      var eatClassArr = [EatClassPageModel]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.title = "食课"
        self.view.backgroundColor = UIColor.whiteColor()
        //        var eatClassTableView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height), style:UITableViewStyle.Plain)
        
        eatClassTableView =  UITableView(frame: CGRectMake(0, 0, BASE_WIDTH, BASE_HEIGHT), style:UITableViewStyle.Plain)
        eatClassTableView.rowHeight = 170
        //        eatClassTableView.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
        eatClassTableView.registerNib(UINib(nibName:"EatClassPageTableViewCell", bundle: nil),forCellReuseIdentifier:"eatClassTableViewCell")
        eatClassTableView.delegate = self
        eatClassTableView.dataSource = self
        
        self.view.addSubview(eatClassTableView)
        createData()
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return eatClassArr.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("eatClassTableViewCell") as! EatClassPageTableViewCell
        
        let model : EatClassPageModel = eatClassArr[indexPath.row]

        cell.createData(model)
        
        
        return cell
    }
    
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let eatClassSecond = EatClassPageSecondViewController()
        
        
        let Model  : EatClassPageModel = eatClassArr[indexPath.row]
        let source = (Model.series_name as NSString).substringWithRange(NSMakeRange(1, (Model.series_name as NSString).length - 1))
        let str = "#"
        let subRange = (source as NSString).rangeOfString(str)   //子范围
        eatClassSecond.title = (source as NSString).substringWithRange(NSMakeRange(0, subRange.location))
        
        eatClassSecond.eatClassModel = Model
        
        
        self.navigationController?.pushViewController(eatClassSecond, animated: true)
        
        
    }
    
    
    func createData() {
        
        let body = "page=1&methodName=CourseIndex&size=20&version=4.3.1&"
            XRAFNetworkTool.POSTUrl(BASE_URL, body: body, requestBodyStyle: XRRequestStyle.String, headFile: nil, response: XRResponseStyle.XRJSON, success: { (dataTask, result) -> Void in
            
                let json = JSON(result)
                
                self.eatClassArr = Array()
                let eatClassArr0 = json["data"]["data"].array
                
                for temp in eatClassArr0!{
                    let dic : Dictionary = temp.dictionaryObject!
                    let model = EatClassPageModel(dic:dic)
                    self.eatClassArr.append(model)
                    
                }
                    self.eatClassTableView.reloadData()
                
                
            print(json) 
        
        }) { (option :NSURLSessionDataTask!, responseObject:NSError!) -> Void in
            
        }
        
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
